package com.teragadgets.android.comics.appengine;

import java.io.IOException;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.servlet.http.*;

@SuppressWarnings("serial")
public class AddComicServlet extends HttpServlet {
	private static final Logger log = Logger.getLogger(AddComicServlet.class
			.getName());

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {

		String name = req.getParameter("name");
		WebComic webComic = new WebComic(name, req.getParameter("url"), req
				.getParameter("favicon"));

		webComic.setNextButtonRegex(req.getParameter("nextButtonRegex"));
		webComic.setComicAltTextRegex(req.getParameter("altTextRegex"));
		webComic.setComicImageRegex(req.getParameter("imageRegex"));
		webComic.setComicNameRegex("nameRegex");
		webComic.setLatestComic(req.getParameter("latestComic"));

		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			pm.makePersistent(webComic);
		} finally {
			pm.close();
		}

		log.info("New Webcomic added: " + name);
		resp.sendRedirect("/index.jsp");
	}
}
